<?php

namespace App\Console\Commands\Fix;

use App\Console\BaseCommand;
use App\Consts\GlobalConst;
use App\Models\SysNote;
use App\Models\SysTopic;
use App\Models\SysTopicTarget;
use Illuminate\Database\Query\Builder;

/**
 * 话题主体修复脚本
 * Class TopicTargetFixCommand
 * @package App\Console\Commands\Fix
 */
class TopicTargetFixCommand extends BaseCommand
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fix:topic {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'fix user_id for sys_topic_target';

    /**
     * 修复 user_id
     * @return int
     */
    public function uid()
    {
        $subMode = $this->_getTargetSubModel();
        $finish = $subMode->count();
        // 每 100 单位合并处理一次
        $subMode->chunkById(100, function ($list) use ($finish) {
            foreach ($list as $item) {
                static $index = 1;
                $id = $item->target_id;
                $note = SysNote::getSubModel(function (Builder $query) use($id) {
                    // $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
                    $query->where('note_id', $id);
                }, ['note_id', 'user_id'])->first();
                if ($note) {
                    $data = [
                        'target_user_id' => $note->user_id
                    ];
                    SysTopicTarget::getModel($item->topic_id)->where('target_id', $id)->update($data);
                    $info = "[$index/$finish] update data to `sys_topic_target` - " . json_encode($data);
                } else {
                    $info = "[$index/$finish] check data - target_id - " . $id;
                }
                // log info
                $this->info($info);
                if ($note) $this->logger($info, false, 'script');
                $index++;
            }
        }, 'target_id');
        return 1;
    }

    /**
     * 修复话题更新时间
     * [!] 将 sys_topic_target 最近一条未被删除的话题的创建时间更新到 sys_topic 的 target_updated_at
     * @return int
     */
    public function uptime()
    {
        $subMode = $this->_getTopicSubModel();
        $finish = $subMode->count();
        // 每 100 单位合并处理一次
        $subMode->chunkById(100, function ($list) use ($finish) {
            foreach ($list as $item) {
                static $index = 1;
                $id = $item->topic_id;
                // 获取最近一条未被删除的话题更新时间
                $res = SysTopicTarget::getModel($id)
                    ->where('topic_id', $id)
                    ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
                    ->orderByDesc('created_at')
                    ->value('created_at');
                if ($res) {
                    $data = [
                        'target_updated_at' => $res
                    ];
                    SysTopic::getModel($id)->where('topic_id', $id)->update($data);
                    $info = "[$index/$finish] update data to `sys_topic` {$id} - " . json_encode($data);
                } else {
                    $info = "[$index/$finish] check data - topic_id - " . $id;
                }
                // log info
                $this->info($info);
                if ($res) $this->logger($info, false, 'script');
                $index++;
            }
        }, 'topic_id');
        return 1;
    }

    /**
     * get sub model
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     */
    private function _getTargetSubModel()
    {
        $subMode = SysTopicTarget::getSubModel(function (Builder $query) {
            $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
            $query->where('target_type', GlobalConst::TARGET_TYPE_NOTE);
            $query->where('target_user_id', '');
        }, ['id', 'target_id', 'topic_id']);
        return $subMode;
    }

    /**
     * get sub model
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     */
    private function _getTopicSubModel()
    {
        $subMode = SysTopic::getSubModel(function (Builder $query) {
            $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
        }, ['id', 'topic_id']);
        return $subMode;
    }

}
